#include <iostream>
#include <vector>

int rank(int key, std::vector<int> a) {
  if (a.empty()) return -1;
  int lo = 0;
  int hi = a.size() - 1;
  while (lo <= hi) {
    int mid = lo + (hi - lo) / 2;
    if (key < a[mid]) 
      hi = mid -1;
    else if (key > a[mid]) 
      lo = mid + 1;
    else 
      return mid;
  }
  return -1;
}

int main() {
  std::vector<int> a = { 1, 2, 3, 9, 20, 21, 32, 56};
  std::cout << "rank: " << rank(56, a) << std::endl;
  return 0;
}
